package likou;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author: 唐小尊
 * @email: 1171702529@qq.com
 * @cate: 2021/09/09 08:19
 */
public class _68 {
    public static void main(String[] args) {
        _68 t = new _68();
        System.out.println(t.fullJustify(new String[]{"Science", "is", "what", "we", "understand", "well", "enough", "to", "explain", "to", "a", "computer.", "Art", "is", "everything", "else", "we", "do"}, 20));
    }

    List<String> res = new ArrayList<>();

    public List<String> fullJustify(String[] words, int maxWidth) {
        List<String> list = new ArrayList<>();
        int num = 0;
        for (String word : words) {
            if (num + list.size() + word.length() > maxWidth) {//list.size()当前需要的最少空格数，三个单词，两个空格
                ru(list, num, maxWidth, false);
                list = new ArrayList<>();
                num = 0;
            }
            num += word.length();
            list.add(word);
        }
        if (list.size() != 0) ru(list, num, maxWidth, true);
        return res;
    }

    public void ru(List<String> list, int num, int maxWidth, boolean down) {
        char[] cs = new char[maxWidth];
        Arrays.fill(cs, ' ');
        int kon = maxWidth - num;

        int p = list.size() < 2 ? 0 : kon / (list.size() - 1);//右边的平均空格数
        int lp = p + 1;//左边的平均空格数
        int lastP = list.size() < 2 ? 0 : kon % (list.size() - 1);//左边的最大空格的个数

        System.out.println(p + " " + lp + " " + lastP);
        int index = 0;
        for (int i = 0; i < list.size(); i++) {
            System.arraycopy(list.get(i).toCharArray(), 0, cs, index, list.get(i).length());
            index += list.get(i).length();
            if (down) {
                index++;
            } else {
                if (lastP > 0) {
                    index += lp;
                    lastP--;
                } else {
                    index += p;
                }
            }
        }
        res.add(new String(cs));
    }
}
